package net.openl10n.mdf;


import java.util.List;

import net.openl10n.document.api.IFragment;
import net.openl10n.document.api.ITextFlow;

import com.google.common.collect.ImmutableList;

public class TextFlow extends AbstractDocumentPart implements ITextFlow{

	private final int hashCode;
	private final ImmutableList<IFragment> fragments;
	private final ImmutableList<PartId> children;
	
	private final String content;
	
	public TextFlow(PartId id, Long version, String content, List<PartId> children, List<IFragment> fragments) {
		super(id, version);
		if(content== null)
			throw new IllegalArgumentException("content cannot be null");
		this.content = content.intern();
		if(children == null)
			throw new IllegalArgumentException("children cannot be null");
		this.children = ImmutableList.copyOf(children);
		if(fragments == null)
			throw new IllegalArgumentException("fragments cannot be null");
		this.fragments = ImmutableList.copyOf(fragments);
		
		this.hashCode = generateHashCode();
	}

	@Override
	public String getContent() {
		return content;
	}
	
	@Override
	public final ImmutableList<IFragment> getFragments() {
		return fragments;
	}
	
	@Override
	public ImmutableList<PartId> getChildren() {
		return children;
	}
	
	@Override
	public final boolean equals(Object obj) {
		if(obj == null) return false;
		if( !(obj instanceof TextFlow) ) return false;
		TextFlow other = (TextFlow) obj;
		return ( super.equals(other) 
				&& other.fragments.equals(fragments) && content.equals(other.content));
	}
	
	@Override
	public final int hashCode() {
		return hashCode;
	}
	
	private final int generateHashCode() {
	    int hash = 1;
	    hash = super.hashCode();
	    hash = hash * 31 + fragments.hashCode();
	    hash = hash * 31 + content.hashCode();
	    return hash;
	}
	
}
